package com.shouhuobao.bhi.receiver;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.order.AppTabFragmentActivity;
import com.collectplus.express.sender.SenderHistoryFragment;
import com.shouhuobao.bhi.login.LoginActivity;

import droid.frame.umeng.UMeng;

/**
 * 添加包裹、最近收件人
 *
 * @author coffee <br>
 * 2016-1-4下午4:07:22
 */
public class ReceiverTabFragmentActivity extends AppTabFragmentActivity {

    /**
     * 0 收件人、1寄件人
     */
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
    }

    /**
     * 是否是添加收件人
     *
     * @return
     */
    public boolean isReceiver() {
        return type == 0;
    }

    protected void initFragment() {
        if (type == 1) {
            getAppTitle().setCommonTitle("添加寄件人");
        } else {
            getAppTitle().setCommonTitle("添加收件人");
        }
        mFragment1 = new ReceiverNewFragment();
        if (1 == type) {
            mFragment2 = new SenderHistoryFragment();
        } else {
            mFragment2 = new ReceiverHistoryFragment();
        }
        mTags[0].setText("新增");
        mTags[1].setText("常用");

        super.mViewPager.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (position == 1) { // 未登录
                    if (isLogin() == false) {
                        Intent intent = new Intent(getContext(), LoginActivity.class);
                        startActivityForResult(intent, 101);
                    }
                }

                if (position == 0) {
                    UMeng.click(getContext(), "receiver_increase_click");
                    mFragment1.setCurrentShow(true);
                } else {
                    UMeng.click(getContext(), "receiver_common_click");
                    mFragment1.setCurrentShow(false);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 从收件人列表 跳转到新增、 编辑寄件人信息
            case MsgID.receiver_edit_history2new:
                selectTab(0);
                return false;
        }
        return super.handleMessage(msg);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK) {
//             showLoadingDialog(null);
            AppHttp.getInstance().senderList(1,type+1);
        }
    }

    @Override
    public void finish() {
        setResult(RESULT_OK);
        super.finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
